<?php

class partenaire_model extends CI_Model{
    
    public function __construct(){
        parent::__construct(); 
        $this->load->database();
        
   }
   
   public function record_count() {
        return $this->db->count_all("partenaire");
    }
    
   public function fetch_partenaire($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("partenaire");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   
   public function getPartenaire($id){
        $limit=1;
        $offset=0;
        $query = $this->db->get_where("partenaire", array('id' => (int)$id), $limit, $offset);
        
        if ($query->num_rows() == 1) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
             return $data[0];
        }
        else
        {
            return false;
        }
   }
   public function getPartenaireByUserId($id){
        $limit=1;
        $offset=0;
        $query = $this->db->get_where("partenaire", array('user_id' => (int)$id), $limit, $offset);
        
        if ($query->num_rows() == 1) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
             return $data[0];
        }
        else
        {
            return false;
        }
   }
   public function getPartenaireEquipement($id){
        
       $this->db->select('equipement.equipement');
       $this->db->from('partenaire_has_equipement');
       $this->db->join('equipement', 'equipement.id = partenaire_has_equipement.equipement_id');
       $this->db->where('partenaire_id', $id); 
       $query = $this->db->get();
       
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
             return $data;
        }
        else
        {
            return false;
        }
   }
   public function getPartenaireMarque($id){
       
       $this->db->select('marque.marque');
       $this->db->from('partenaire_has_marque');
       $this->db->join('marque', 'marque.id = partenaire_has_marque.marque_id');
       $this->db->where('partenaire_id', $id); 
       $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
             return $data;
        }
        else
        {
            return false;
        }
   }
   
   public function updatePartenaire($id){
        $partenaire_data = array(
                'adresse1' => $_POST['adresse1'],
                'adresse2' =>  $_POST['adresse2'],
                'codepostal' => $_POST['codepostal'],
                'ville' => $_POST['ville'],
                'pays' => $_POST['pays'],
                'telephone' =>  $_POST['telephone'],
                'fax' =>  $_POST['fax'],

            );
        $this->db->where('user_id', $id);
        if($this->db->update('partenaire', $partenaire_data)){
            return true;
        } else{
            return false;
        }
    }
    
    
     public function updatePartenaireDescription($id){
        $partenaire_data = array(
                'horaires' => $_POST['horaires'],
                'description' =>  $_POST['description']
            );
        $this->db->where('user_id', $id);
        if($this->db->update('partenaire', $partenaire_data)){
            return true;
        } else{
            return false;
        }
    }
    
    public function deletePartenaireEquipement($idPartenaire,$idEquipement){
        $partenaire_data = array(
            'partenaire_id' => $idPartenaire,
            'equipement_id' => $idEquipement
        );
        if($this->delete('partenaire_has_equipement',$partenaire_data)){
            return true;
        }else{
            return false;
        }
    }
}
?>
